package com.dhsc.service.impl.jyjk.zxyc;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.dhsc.domain.jkzx.zxjh.YcjkyzVo;
import com.dhsc.domain.jkzx.zxjh.ZhgVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.yhzx.TJgxxVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.yypt.IYhzxDao;
import com.dhsc.service.jyjk.zxyc.IZhgJkService;
import com.dhsc.util.CommonUtil;
import com.dhsc.view.dao.jyjk.zxyc.IZhgDao;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName:  ZhgJkController
 * @Description: 正回购监控
 * @date:   2023年7月31日 上午9:13:42
 * @author: clong
 * @Copyright:  东海运营
 */
@Service
@Slf4j
public class ZhgJkServiceImpl implements IZhgJkService {

    @Autowired
    private IZhgDao zhgDao;

    @Autowired
    protected JdbcTemplate jdbcTemplate;

	@Autowired
	private IYhzxDao yhzxDao;
	
	private static final String BZQSYLSH = "BZQSYLSH";
	
	private static final String BZQSYLSZ = "BZQSYLSZ";
	
	private static final String WDQBL = "WDQBL";
	
	private static final String RKJZD = "RKJZD";
	
	private static final String FONTRED = "<font style='color:red'>";
	
	private static final String FONTEND = "</font>";
	
	private static final String FONTPINK = "<font style='color:pink'>";

    @Override
    public LayuiTableVo selectZzhgInfo(ZhgVo z) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(z.getPage(), z.getLimit());
            List<ZhgVo> list = zhgDao.selectZzhgInfo(z);
            PageInfo<ZhgVo> pageInfo = new PageInfo<>(list);
            
            if(CommonUtil.isNotEmptyList(list))
			{
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				
				for(int i = 0;i < list.size();i ++)
				{
					for(int j = 0;j < jglist.size();j ++)
					{
						if(list.get(i).getYyb().equals(jglist.get(j).getJgdm()))
						{
							list.get(i).setBmmc(jglist.get(j).getBmmc());
						}
					}
				}
			}
            
            List<YcjkyzVo> yzlist = zhgDao.queryfzxx();
            //颜色阀值
            ssfzdyys(yzlist,list);
            
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch(Exception e){
            log.error("查专项监控正回购出错", e);
            throw new MyException("查专项监控正回购出错",e);
        }
        return obj;
    }

    public List<ZhgVo> ssfzdyys(List<YcjkyzVo> zlist,List<ZhgVo> list){

        //客户透支
        YcjkyzVo  zt = dyfzxx(zlist,"KHTZP",3);
        Double kyzj = Double.valueOf(zt.getToplimit());
        //标准券使用率_上海
        Double sylsh=0.00;
        YcjkyzVo zsh = dyfzxx(zlist,BZQSYLSH,3);
        if(CommonUtil.isNotEmptyObj(zsh.getLowerlimit())){
            sylsh = Double.valueOf(zsh.getLowerlimit());
        }
        
        //标准券使用率_上海
        Double sylsz = 0.00;
        YcjkyzVo  zsz = dyfzxx(zlist,BZQSYLSZ,3);
        if(CommonUtil.isNotEmptyObj(zsz.getLowerlimit())){
             sylsz = Double.valueOf(zsz.getLowerlimit());
        }
        //未到期比率
        Double tgbl = 0.00;
        YcjkyzVo  zw = dyfzxx(zlist,WDQBL,3);
        if(CommonUtil.isNotEmptyObj(zw.getLowerlimit())){
            tgbl = Double.valueOf(zw.getLowerlimit());
        }
        //未到期比率
        Double jzd= 0.00;
        YcjkyzVo  zj = dyfzxx(zlist,RKJZD,3);
        if(CommonUtil.isNotEmptyObj(zj.getLowerlimit())){
            jzd = Double.valueOf(zj.getLowerlimit());
        }
        int cv =100;
        if (CommonUtil.isNotEmptyList(list)) {
            for (ZhgVo z : list) {
                //客户透支
                if (Double.valueOf(z.getKyzj())<kyzj) {
                    Double  kyzjval = Double.valueOf(z.getKyzj());
                    BigDecimal bg = BigDecimal.valueOf(kyzjval);
                    kyzjval = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    z.setKyzj(FONTRED + CommonUtil.convert(kyzjval) + FONTEND);
                }else{
                    z.setKyzj(CommonUtil.convert(Double.valueOf(z.getKyzj()))+"");
                }
                //标准券使用率_上海
                Double sylshvl = Double.valueOf(z.getSyl_sh());
                if (sylshvl>=sylsh) {
                    z.setSyl_sh(fontColor("red",CommonUtil.convert(sylshvl*cv).doubleValue()));
                }else{
                    z.setSyl_sh(Double.valueOf(sylshvl*cv)+"%");
                }
                //标准券使用率_深圳
                Double sylszval = Double.valueOf(z.getSyl_sz());
                if (sylszval>=sylsz) {
                    z.setSyl_sz(fontColor("red",CommonUtil.convert(sylszval*cv).doubleValue()));
                }else{
                    z.setSyl_sz(CommonUtil.convert(sylszval*cv)+"%");
                }
                //托管比例
                Double tgblval = Double.valueOf(z.getTgbl());
                if(tgblval>=tgbl){
                    z.setTgbl(fontColor("red",CommonUtil.convert(tgblval*cv).doubleValue()));
                }else{
                    z.setTgbl(CommonUtil.convert(tgblval*cv)+"%");
                }

                //集中度_入库
                if (CommonUtil.isNotEmptyObj(z.getJzd_rk())) {
                    String[] arr = z.getJzd_rk().split(",");
                    for (String s : arr) {
                        Double jzdval =Double.valueOf(CommonUtil.subBetween(s).substring(0,CommonUtil.subBetween(s).length()-1));
                        BigDecimal bg = BigDecimal.valueOf(jzdval/100);
                        jzdval = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                        if (jzdval>=jzd) {
                            z.setJzd_rk(FONTRED+z.getJzd_rk()+FONTEND);
                            break;
                        }
                    }
                }

            }
        }

        return list;
    }

    /**
     * @Description:设置颜色
     * @return: java.lang.String
     * @Author: CDW
     * @Date: 2023/9/11 10:48
     * @param color:
     * @param val:
     */
    public static String  fontColor(String color,Double val){
        return "<font style='color:" + color + "'>" + val + "%</font>";
    }

    /**
     * @Description:获取阀值
     * @return: com.dhsc.domain.jkzx.zxjh.YcjkyzVo
     * @Author: CDW
     * @Date: 2023/9/11 10:43
     * @param zlist:
     * @param pname:
     * @param type:
     */
    private YcjkyzVo dyfzxx(List<YcjkyzVo> zlist, String pname, int type) {
        YcjkyzVo ycjkyzVo = new YcjkyzVo();
        if (CommonUtil.isNotEmptyList(zlist)) {
            for (YcjkyzVo z : zlist) {
                if (z.getPname().equals(pname) && z.getType()==type) {
                	ycjkyzVo.setLowerlimit(z.getLowerlimit());
                	ycjkyzVo.setToplimit(z.getToplimit());
                }
            }
        }
        return ycjkyzVo;
    }

    @Override
    public LayuiTableVo queryzhgjkmxurl(ZhgVo z) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(z.getPage(), z.getLimit());
            List<ZhgVo> list = zhgDao.queryzhgjkmx(z);
            PageInfo<ZhgVo> pageInfo = new PageInfo<>(list);
            
            if(CommonUtil.isNotEmptyList(list))
			{
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				
				for(int i = 0;i < list.size();i ++)
				{
					for(int j = 0;j < jglist.size();j ++)
					{
						if(list.get(i).getYyb().equals(jglist.get(j).getJgdm()))
						{
							list.get(i).setBmmc(jglist.get(j).getBmmc());
						}
					}
				}
			}
            
            List<YcjkyzVo> yzlist = zhgDao.queryfzxx();
            //颜色阀值
            zhgmxfzdyys(yzlist,list);
            
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch(Exception e){
            log.error("查正回购监控明细出错", e);
            throw new MyException("查正回购监控明细出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryzhgydjkmxurl(ZhgVo z) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(z.getPage(), z.getLimit());
            List<ZhgVo> list = zhgDao.queryzhgydjkmx(z);
            PageInfo<ZhgVo> pageInfo = new PageInfo<>(list);
            
            if(CommonUtil.isNotEmptyList(list))
			{
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				
				for(int i = 0;i < list.size();i ++)
				{
					for(int j = 0;j < jglist.size();j ++)
					{
						if(list.get(i).getYyb().equals(jglist.get(j).getJgdm()))
						{
							list.get(i).setBmmc(jglist.get(j).getBmmc());
						}
					}
				}
			}
            
            List<YcjkyzVo> yzlist = zhgDao.queryfzxx();
            //颜色阀值
            mmfzdyys(yzlist,list);
            
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch(Exception e){
            log.error("查正回购月度监控明细出错", e);
            throw new MyException("查正回购月度监控明细出错",e);
        }
        return obj;
    }

    /**
     * @Description:月度阀值对应颜色管理
     * @return: java.util.List<com.dhsc.domain.jkzx.zxjh.ZhgVo>
     * @Author: CDW 
     * @Date: 2023/9/12 13:56
     * @param zlist: 
     * @param list: 
     */
    public List<ZhgVo> mmfzdyys(List<YcjkyzVo> zlist,List<ZhgVo> list){

        //临近收盘笔数
        YcjkyzVo  zljspbs = dyfzxx(zlist,"LJSPBS",2);
        int ljspbssx =Integer.parseInt(zljspbs.getToplimit());
        int ljspbsxx =Integer.parseInt(zljspbs.getLowerlimit());
        YcjkyzVo  zljspbsf = dyfzxx(zlist,"LJSPBS",3);
        int ljspbsgfx = Integer.parseInt(zljspbsf.getLowerlimit());
        //较高价格笔数
        YcjkyzVo  zjgjg = dyfzxx(zlist,"JGJGBS",2);
        int jgjgbssx =Integer.parseInt(zjgjg.getToplimit());
        int jgjgbsxx =Integer.parseInt(zjgjg.getLowerlimit());
        YcjkyzVo  zjgjgf = dyfzxx(zlist,"JGJGBS",3);
        int jgjgbsgfx = Integer.parseInt(zjgjgf.getLowerlimit());
        //净值波动
        YcjkyzVo  zjzbd = dyfzxx(zlist,"JZBD",2);
        Double jzbdsx = Double.valueOf(zjzbd.getToplimit());
        Double jzbdxx = Double.valueOf(zjzbd.getLowerlimit());
        YcjkyzVo  zjzbdf = dyfzxx(zlist,"JZBD",3);
        Double jzbdgfx = Double.valueOf(zjzbdf.getLowerlimit());

        int cv =100;
        if (CommonUtil.isNotEmptyList(list)) {
            for (ZhgVo z : list) {
                //临近收盘笔数
                int ljspval = Integer.parseInt(z.getLjsp());
                if(ljspval>=ljspbsgfx){
                    z.setLjsp(FONTRED+ljspval+FONTEND);
                }else if(ljspval>=ljspbsxx && ljspval<ljspbssx){
                    z.setLjsp(FONTPINK+ljspval+FONTEND);
                }
                //较高价格笔数
                int jgjgval = Integer.parseInt(z.getJgjg());
                if(jgjgval>=jgjgbsgfx){
                    z.setJgjg(FONTRED+jgjgval+FONTEND);
                }else if(jgjgval>=jgjgbsxx && jgjgval<jgjgbssx){
                    z.setJgjg(FONTPINK+jgjgval+FONTEND);
                }
                //净值波动
                Double jzbdlval = Double.valueOf(z.getJzbdl());
                if(jzbdlval>=jzbdgfx){
                    z.setJzbdl(fontColor("red",CommonUtil.szgsh(jzbdlval*cv)));
                }else if(jzbdlval>=jzbdxx && jzbdlval<jzbdsx){
                    z.setJzbdl(fontColor("pink",CommonUtil.szgsh(jzbdlval*cv)));
                }else{
                    z.setJzbdl(CommonUtil.szgsh(jzbdlval*cv)+"%");
                }

            }
        }
        return list;
    }
    
    public List<ZhgVo> zhgmxfzdyys(List<YcjkyzVo> zlist,List<ZhgVo> list){

        //客户透支
        YcjkyzVo  zt = dyfzxx(zlist,"KHTZP",3);
        Double kyzjgfx = Double.valueOf(zt.getToplimit());
        //标准券使用率_上海
        YcjkyzVo  zsh = dyfzxx(zlist,BZQSYLSH,2);
        Double sylshzfxsx = Double.valueOf(zsh.getToplimit());
        Double sylshzfxxx = Double.valueOf(zsh.getLowerlimit());
        YcjkyzVo  zshg = dyfzxx(zlist,BZQSYLSH,3);
        Double sylshgfx = Double.valueOf(zshg.getLowerlimit());
        //标准券使用率_深圳
        YcjkyzVo  zsz = dyfzxx(zlist,BZQSYLSZ,2);
        Double sylszzfxsx = Double.valueOf(zsz.getToplimit());
        Double sylszzfxxx = Double.valueOf(zsz.getLowerlimit());
        YcjkyzVo  zsg = dyfzxx(zlist,BZQSYLSZ,3);
        Double sylszgfx = Double.valueOf(zsg.getLowerlimit());
        //未到期比率
        YcjkyzVo  zw = dyfzxx(zlist,WDQBL,2);
        Double tgblzfxsx = Double.valueOf(zw.getToplimit());
        Double tgblzfxxx = Double.valueOf(zw.getLowerlimit());
        YcjkyzVo  zwg = dyfzxx(zlist,WDQBL,3);
        Double tgblgfx = Double.valueOf(zwg.getLowerlimit());
        //未到期比率
        YcjkyzVo  zj = dyfzxx(zlist,RKJZD,2);
        Double jdzrkzfxsx = Double.valueOf(zj.getToplimit());
        Double jdzrkzfxxx = Double.valueOf(zj.getLowerlimit());
        YcjkyzVo  zjg = dyfzxx(zlist,RKJZD,3);
        Double jdzrkgfx = Double.valueOf(zjg.getLowerlimit());
        //回购杠杆率
        YcjkyzVo  zggl = dyfzxx(zlist,"HGGGL",2);
        Double hggglzfxsx = Double.valueOf(zggl.getToplimit());
        Double hggglzfxxx = Double.valueOf(zggl.getLowerlimit());
        YcjkyzVo  zgglfx = dyfzxx(zlist,"HGGGL",3);
        Double hggglgfx = Double.valueOf(zgglfx.getLowerlimit());
        //质押券占比
        YcjkyzVo  zyqzb = dyfzxx(zlist,"ZYQZB",2);
        Double zyqzbzfxsx = Double.valueOf(zyqzb.getToplimit());
        Double zyqzbzfxxx = Double.valueOf(zyqzb.getLowerlimit());
        YcjkyzVo  zyqzbf = dyfzxx(zlist,"ZYQZB",3);
        Double zyqzbgfx = Double.valueOf(zyqzbf.getLowerlimit());
        //高流动资产占比
        YcjkyzVo  gldzczb = dyfzxx(zlist,"GLDZCZB",2);
        Double gldzczbzfxsx = Double.valueOf(gldzczb.getToplimit());
        Double gldzczbzfxxx = Double.valueOf(gldzczb.getLowerlimit());
        YcjkyzVo  gldzczbf = dyfzxx(zlist,"GLDZCZB",3);
        Double gldzczbgfx = Double.valueOf(gldzczbf.getToplimit());
        //转出资金占比
        YcjkyzVo  zczjzb = dyfzxx(zlist,"ZCZJZB",2);
        Double zczjzbzfxsx = Double.valueOf(zczjzb.getToplimit());
        Double zczjzbzfxxx = Double.valueOf(zczjzb.getLowerlimit());
        YcjkyzVo  zczjzbf = dyfzxx(zlist,"ZCZJZB",3);
        Double zczjzbgfx = Double.valueOf(zczjzbf.getLowerlimit());
        //风险资产占比
        YcjkyzVo  fxzczb = dyfzxx(zlist,"FXZCZB",2);
        Double fxzczbzfxsx = Double.valueOf(fxzczb.getToplimit());
        Double fxzczbzfxxx = Double.valueOf(fxzczb.getLowerlimit());
        YcjkyzVo  fxzczbf = dyfzxx(zlist,"FXZCZB",3);
        Double fxzczbgfx = Double.valueOf(fxzczbf.getLowerlimit());

        int cv =100;
        if (CommonUtil.isNotEmptyList(list)) {
            for (ZhgVo z : list) {
                //客户透支
                Double kyzjval = Double.valueOf(z.getKyzj());
                if (kyzjval<kyzjgfx) {
                    z.setKyzj(FONTRED+kyzjval+FONTEND);
                }else{
                    z.setKyzj(CommonUtil.convert(kyzjval)+"");
                }
                //标准券使用率_上海
                Double syslsh = Double.valueOf(z.getSyl_sh());
                if (syslsh>=sylshgfx) {
                    z.setSyl_sh(fontColor("red",CommonUtil.szgsh(syslsh*cv)));
                }else if(syslsh>=sylshzfxxx && syslsh<sylshzfxsx){
                    z.setSyl_sh(fontColor("pink",CommonUtil.szgsh(syslsh*cv)));
                }else {
                    z.setSyl_sh(CommonUtil.szgsh(syslsh*cv)+"%");
                }
                //标准券使用率_深圳
                Double syslsz = Double.valueOf(z.getSyl_sz());
                if (syslsz>=sylszgfx) {
                    z.setSyl_sz(fontColor("red",CommonUtil.szgsh(syslsz*cv)));
                }else if(syslsz>=sylszzfxxx && syslsz<sylszzfxsx){
                    z.setSyl_sz(fontColor("pink",CommonUtil.szgsh(syslsz*cv)));
                }else {
                    z.setSyl_sz(CommonUtil.szgsh(syslsz*cv)+"%");
                }
                //托管比例
                Double tgbl = Double.valueOf(z.getTgbl());
                if(tgbl>=tgblgfx){
                    z.setTgbl(fontColor("red",CommonUtil.szgsh(tgbl*cv)));
                }else if(tgbl>=tgblzfxxx && tgbl<tgblzfxsx){
                    z.setTgbl(fontColor("pink",CommonUtil.szgsh(tgbl*cv)));
                }else{
                    z.setTgbl(CommonUtil.szgsh(tgbl*cv)+"%");
                }
                //集中度_入库
                if (CommonUtil.isNotEmptyObj(z.getJzd_rk())) {
                    String[] arr = z.getJzd_rk().split(",");
                    for (String s : arr) {
                        Double jzdval =Double.valueOf(CommonUtil.subBetween(s).substring(0,CommonUtil.subBetween(s).length()-1));
                        BigDecimal bg = BigDecimal.valueOf(jzdval/100);
                        jzdval = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                        if (jzdval>=jdzrkgfx) {
                            z.setJzd_rk(FONTRED+z.getJzd_rk()+FONTEND);
                            break;
                        }else if(jzdval>=jdzrkzfxxx && jzdval<jdzrkzfxsx){
                            z.setJzd_rk(FONTPINK+z.getJzd_rk()+FONTEND);
                            break;
                        }
                    }
                }
                //回购杠杆率
                Double jzcbl = Double.valueOf(z.getJzcbl());
                if(jzcbl>=hggglgfx){
                    z.setJzcbl(fontColor("red",CommonUtil.szgsh(jzcbl*cv)));
                }else if(jzcbl>=hggglzfxxx && jzcbl<hggglzfxsx){
                    z.setJzcbl(fontColor("pink",CommonUtil.szgsh(jzcbl*cv)));
                }else{
                    z.setJzcbl(CommonUtil.szgsh(jzcbl*cv)+"%");
                }
                //担保债券集中度
                if (CommonUtil.isNotEmptyObj(z.getJzd_db())) {
                    String[] arr = z.getJzd_db().split(",");
                    for (String s : arr) {
                        Double jzdval =Double.valueOf(CommonUtil.subBetween(s).substring(0,CommonUtil.subBetween(s).length()-1));
                        BigDecimal bg = BigDecimal.valueOf(jzdval/100);
                        jzdval = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                        if (jzdval >= zyqzbgfx) {
                            z.setJzd_db(FONTRED+z.getJzd_db()+FONTEND);
                            break;
                        }else if(jzdval >= zyqzbzfxxx && jzdval < zyqzbzfxsx){
                            z.setJzd_db(FONTPINK+z.getJzd_db()+FONTEND);
                            break;
                        }
                    }
                }
                //高流动资产占比
                Double gldbl = Double.valueOf(z.getGldbl());
                if(gldbl<gldzczbgfx){
                    z.setGldbl(fontColor("red",CommonUtil.szgsh(gldbl*cv)));
                }else if(gldbl>=gldzczbzfxxx && gldbl<gldzczbzfxsx){
                    z.setGldbl(fontColor("pink",CommonUtil.szgsh(gldbl*cv)));
                }else{
                    z.setGldbl(CommonUtil.szgsh(gldbl*cv)+"%");
                }
                //转出资金占比
                Double zczjbl = Double.valueOf(z.getZczjbl());
                if(zczjbl>=zczjzbgfx){
                    z.setZczjbl(fontColor("red",CommonUtil.szgsh(zczjbl*cv)));
                }else if(zczjbl>=zczjzbzfxxx && zczjbl<zczjzbzfxsx){
                    z.setZczjbl(fontColor("pink",CommonUtil.szgsh(zczjbl*cv)));
                }else{
                    z.setZczjbl(CommonUtil.szgsh(zczjbl*cv)+"%");
                }
                //风险资产占比
                Double fxzcbl = Double.valueOf(z.getFxzcbl());
                if(fxzcbl>=fxzczbgfx){
                    z.setFxzcbl(fontColor("red",CommonUtil.szgsh(fxzcbl*cv)));
                }else if(fxzcbl>=fxzczbzfxxx && fxzcbl<fxzczbzfxsx){
                    z.setFxzcbl(fontColor("pink",CommonUtil.szgsh(fxzcbl*cv)));
                }else{
                    z.setFxzcbl(CommonUtil.szgsh(fxzcbl*cv)+"%");
                }
            }
        }

        return list;

    }
}
